<template>
  <div class="card-block">
    <!-- 编辑模式 -->
    <div v-if="!previewMode" class="card-editor">
      <el-form label-position="top" size="small">
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item label="卡片标题">
              <el-input
                v-model="localContent.title"
                placeholder="请输入卡片标题"
                @input="handleUpdate"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="副标题">
              <el-input
                v-model="localContent.subtitle"
                placeholder="请输入副标题（可选）"
                @input="handleUpdate"
              />
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-form-item label="描述内容">
          <el-input
            v-model="localContent.description"
            type="textarea"
            :rows="3"
            placeholder="请输入描述内容"
            @input="handleUpdate"
          />
        </el-form-item>
        
        <!-- 移除图片和链接相关的表单项 -->
        
        <!-- 卡片内容设置 -->
        <el-divider content-position="left">卡片设置</el-divider>
        
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item label="卡片主题">
              <el-select
                v-model="localContent.theme"
                @change="handleUpdate"
              >
                <el-option label="默认" value="default" />
                <el-option label="简约" value="minimal" />
                <el-option label="现代" value="modern" />
                <el-option label="渐变" value="gradient" />
                <el-option label="FAQ问答" value="faq" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item label="悬停效果">
              <el-switch
                v-model="localContent.hoverEffect"
                @change="handleUpdate"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="显示按钮">
              <el-switch
                v-model="localContent.showButton"
                @change="handleUpdate"
              />
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-form-item v-if="localContent.showButton" label="按钮文字">
          <el-input
            v-model="localContent.buttonText"
            placeholder="了解更多"
            @input="handleUpdate"
          />
        </el-form-item>
        
        <!-- FAQ专用字段 -->
        <template v-if="localContent.theme === 'faq'">
          <el-divider content-position="left">FAQ设置</el-divider>
          <el-row :gutter="16">
            <el-col :span="8">
              <el-form-item label="问题编号">
                <el-input-number
                  v-model="localContent.questionNumber"
                  :min="1"
                  :max="99"
                  @change="handleUpdate"
                />
              </el-form-item>
            </el-col>
            <el-col :span="16">
              <el-form-item label="问题标题">
                <el-input
                  v-model="localContent.question"
                  placeholder="请输入问题标题"
                  @input="handleUpdate"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="答案内容">
            <el-input
              v-model="localContent.answer"
              type="textarea"
              :rows="4"
              placeholder="请输入详细答案"
              @input="handleUpdate"
            />
          </el-form-item>
        </template>
      </el-form>
    </div>

    <!-- 预览模式 -->
    <div v-else class="card-preview">
      <div 
        class="card-container"
        :class="[
          `theme-${localContent.theme}`,
          { 'hover-effect': localContent.hoverEffect }
        ]"
        @click="handleCardClick"
      >
        
        <!-- FAQ主题内容 -->
        <div v-if="localContent.theme === 'faq'" class="faq-content">
          <div class="faq-header">
            <div class="question-number">
              {{ localContent.questionNumber || 1 }}
            </div>
            <div class="question-text">
              <h3 class="faq-question">{{ localContent.question || localContent.title || '问题标题' }}</h3>
            </div>
          </div>
          <div class="faq-answer">
            <div class="answer-content">
              <p v-for="(paragraph, index) in getAnswerParagraphs()" :key="index">
                {{ paragraph }}
              </p>
            </div>
          </div>
        </div>
        
        <!-- 普通主题内容部分 -->
        <div class="card-content" v-else>
          <div class="card-header">
            <h3 v-if="localContent.title" class="card-title">{{ localContent.title }}</h3>
            <p v-if="localContent.subtitle" class="card-subtitle">{{ localContent.subtitle }}</p>
          </div>
          
          <div v-if="localContent.description" class="card-description">
            <p>{{ localContent.description }}</p>
          </div>
          
          <div v-if="localContent.showButton" class="card-actions">
            <el-button type="primary" size="small">
              {{ localContent.buttonText || '了解更多' }}
              <el-icon><ArrowRight /></el-icon>
            </el-button>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 移除图片上传对话框 -->
  </div>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { ArrowRight } from '@element-plus/icons-vue'

// Props
const props = defineProps({
  block: {
    type: Object,
    required: true
  },
  previewMode: {
    type: Boolean,
    default: false
  }
})

// Emits
const emit = defineEmits(['update'])

/**
 * 获取默认内容结构
 * @returns {Object} 默认的卡片内容配置
 */
const getDefaultContent = () => ({
  title: '卡片标题',
  subtitle: '',
  description: '这里是卡片的描述内容，可以详细介绍相关信息。',
  theme: 'default',
  hoverEffect: true,
  showButton: true,
  buttonText: '了解更多',
  // FAQ专用字段
  questionNumber: 1,
  question: '',
  answer: ''
})

// 本地内容状态
const localContent = ref({
  ...getDefaultContent(),
  ...props.block.content
})

// 移除上传相关状态

/**
 * 处理内容更新
 */
const handleUpdate = () => {
  emit('update', {
    content: { ...localContent.value }
  })
}

// 移除图片上传相关方法

/**
 * 获取答案段落数组
 * @returns {Array} 答案段落数组
 */
const getAnswerParagraphs = () => {
  const answer = localContent.value.answer || localContent.value.description || ''
  return answer.split('\n').filter(paragraph => paragraph.trim())
}

/**
 * 处理卡片点击
 */
const handleCardClick = () => {
  // 移除链接跳转功能
}

// 监听 props 变化
watch(
  () => props.block.content,
  (newContent) => {
    localContent.value = {
      ...localContent.value,
      ...newContent
    }
  },
  { deep: true }
)
</script>

<style scoped>
.card-block {
  width: 100%;
}

.card-editor {
  padding: 16px;
  background: #f8f9fa;
  border-radius: 8px;
  border: 1px solid #e9ecef;
}

.card-preview {
  width: 100%;
}

.card-container {
  border-radius: 12px;
  overflow: hidden;
  transition: all 0.3s ease;
  cursor: pointer;
  height: 100%;
}

/* 移除布局和图片位置样式 */

/* 主题样式 */
.theme-default {
  background: white;
  border: 1px solid #e4e7ed;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.theme-minimal {
  background: white;
  border: none;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.theme-modern {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
}

.theme-gradient {
  background: linear-gradient(135deg, #ff9a9e 0%, #fecfef 50%, #fecfef 100%);
  border: none;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

.theme-faq {
  background: #f8faff;
  border: 1px solid #e1e8f5;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(59, 130, 246, 0.08);
}

/* 悬停效果 */
.hover-effect:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

/* 移除图片样式 */

/* 移除覆盖层相关样式 */

/* FAQ内容样式 */
.faq-content {
  padding: 24px;
}

.faq-header {
  display: flex;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 16px;
}

.question-number {
  width: 32px;
  height: 32px;
  background: #3b82f6;
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 14px;
  flex-shrink: 0;
}

.question-text {
  flex: 1;
}

.faq-question {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: #1f2937;
  line-height: 1.4;
}

.faq-answer {
  margin-left: 48px;
}

.answer-content {
  color: #4b5563;
  line-height: 1.6;
}

.answer-content p {
  margin: 0 0 12px 0;
}

.answer-content p:last-child {
  margin-bottom: 0;
}

/* 普通内容样式 */
.card-content {
  padding: 20px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  flex: 1;
}

.card-header {
  margin-bottom: 12px;
}

.card-title {
  margin: 0 0 8px 0;
  font-size: 20px;
  font-weight: 600;
  line-height: 1.3;
}

.card-subtitle {
  margin: 0;
  color: #666;
  font-size: 14px;
}

.theme-modern .card-subtitle {
  color: rgba(255, 255, 255, 0.8);
}

.card-description {
  flex: 1;
  margin-bottom: 16px;
}

.card-description p {
  margin: 0;
  line-height: 1.6;
  color: #666;
}

.theme-modern .card-description p {
  color: rgba(255, 255, 255, 0.9);
}

.card-actions {
  margin-top: auto;
}

/* 上传组件样式 */
.upload-demo {
  width: 100%;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .layout-horizontal {
    flex-direction: column;
  }
  
  .image-left .card-image,
  .image-right .card-image {
    order: 1;
    flex: none;
  }
  
  .image-left .card-content,
  .image-right .card-content {
    order: 2;
    flex: none;
  }
  
  .card-content {
    padding: 16px;
  }
  
  .card-title {
    font-size: 18px;
  }
  
  /* FAQ响应式样式 */
  .faq-content {
    padding: 16px;
  }
  
  .faq-header {
    gap: 12px;
    margin-bottom: 12px;
  }
  
  .question-number {
    width: 28px;
    height: 28px;
    font-size: 12px;
  }
  
  .faq-question {
    font-size: 15px;
  }
  
  .faq-answer {
    margin-left: 40px;
  }
  
  .answer-content {
    font-size: 14px;
  }
}
</style>